package com.fyzk.activity.popupwindow;

import android.animation.Animator;
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.utils.LogUtil;
import com.tyjc.lottery.R;

import http.DataUtil.text.TextUtil;
import razerdp.basepopup.BasePopupWindow;

/**
 * 银行卡转卡填写转款人姓名弹窗弹窗
 */
public class YHKZKPopup extends BasePopupWindow implements View.OnClickListener {

    private EditText mEtContent;
    private TextView mTvCancel, mTvConfirm;

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.layout_popup_yhkzk);
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return getDefaultScaleAnimation(false);
    }

    public YHKZKPopup(Context context) {
        super(context);
        setPopupGravity(Gravity.CENTER);
        setAdjustInputMethod(true);

        initView();
        initListener();

        setAutoShowInputMethod(mEtContent, true);
    }

    private void initView() {
        mEtContent = findViewById(R.id.et_content);
        mTvCancel = findViewById(R.id.tv_cancel);
        mTvConfirm = findViewById(R.id.tv_confirm);
    }

    private void initListener() {
        mEtContent.setFilters(new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
            String sText = source.toString();
            if (TextUtil.isEmpty(sText))
                return "";
            char[] chars = sText.toCharArray();
            StringBuilder newSource = new StringBuilder();
            for (char aChar : chars) {
                if (!((aChar >= '0' && aChar <= '9')
                        || (aChar >= 'a' && aChar <= 'z')
                        || (aChar >= 'A' && aChar <= 'Z')
                        || (aChar == ' '))) {
                    newSource.append(aChar);
                }
            }
            LogUtil.e(newSource.toString());
            return newSource.toString().replaceAll(" ", "");
        }});

        mTvCancel.setOnClickListener(this);
        mTvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                if (mOnClickListener != null)
                    mOnClickListener.onCancelClick(YHKZKPopup.this, v);
                dismiss();
                break;
            case R.id.tv_confirm://确定
                String content = mEtContent.getText().toString().trim().replaceAll(" ", "");
                if (TextUtil.isEmpty(content)) {
                    AppToast.getToast().show("请填写转款人姓名");
                    return;
                }
                if (content.length()<2 || content.length()>10){
                    AppToast.getToast().show("姓名长度为2-10个字");
                    return;
                }
                if (mOnClickListener != null)
                    mOnClickListener.onConfirmClick(YHKZKPopup.this, v, content);
                break;
        }
    }

    private void clearContent() {
        mEtContent.setText("");
    }

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        clearContent();
    }

    @Override
    public void showPopupWindow(int anchorViewResid) {
        super.showPopupWindow(anchorViewResid);
        clearContent();
    }

    @Override
    public void showPopupWindow(View anchorView) {
        super.showPopupWindow(anchorView);
        clearContent();
    }

    @Override
    public void showPopupWindow(int x, int y) {
        super.showPopupWindow(x, y);
        clearContent();
    }

    private onClickListener mOnClickListener;

    public void setOnClickListener(onClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    public interface onClickListener {
        void onCancelClick(BasePopupWindow popupWindow, View view);

        void onConfirmClick(BasePopupWindow popupWindow, View view, String content);
    }
}
